package cn.edu.pzhu.util;

import java.text.Collator;
import java.util.Comparator;
import java.util.Locale;

import cn.edu.pzhu.pojo.FileMsg;

public class ListSortUtil {
	public Comparator<FileMsg> getComparator(String key) {
		if ("username".equals(key)) {
			return new UsernameSort() ;
		}
		if ("filename".equals(key)) {
			return new FilenameSort() ;
		}
		if ("classification".equals(key)) {
			return new ClassSort() ;
		}
		if ("releasedate".equals(key)) {
			return new DateSort() ;
		}
		if ("rating".equals(key)) {
			return new RatingSort() ;
		}
		if ("description".equals(key)) {
			return new DescriptionSort() ;
		}
		return new FilenameSort() ;
	}

	class UsernameSort implements Comparator<FileMsg>{

		@Override
		public int compare(FileMsg o1, FileMsg o2) {
			Collator instance = Collator.getInstance(Locale.CHINA);
			return instance.compare(o1.getUsername(), o2.getUsername());
		}		
	}
	class FilenameSort implements Comparator<FileMsg>{

		@Override
		public int compare(FileMsg o1, FileMsg o2) {
			Collator instance = Collator.getInstance(Locale.CHINA);
			return instance.compare(o1.getFilename(), o2.getFilename());
		}		
	}
	class ClassSort implements Comparator<FileMsg>{

		@Override
		public int compare(FileMsg o1, FileMsg o2) {
			Collator instance = Collator.getInstance(Locale.CHINA);
			return instance.compare(o1.getClassification(), o2.getClassification());			
		}		
	}
	class DateSort implements Comparator<FileMsg>{

		@Override
		public int compare(FileMsg o1, FileMsg o2) {
			return -o1.getReleasedate().compareTo(o2.getReleasedate());
		}		
	}
	class RatingSort implements Comparator<FileMsg>{

		@Override
		public int compare(FileMsg o1, FileMsg o2) {
			return -o1.getRating().compareTo(o2.getRating());
		}		
	}
	class DescriptionSort implements Comparator<FileMsg>{

		@Override
		public int compare(FileMsg o1, FileMsg o2) {
			Collator instance = Collator.getInstance(Locale.CHINA);
			return instance.compare(o1.getDescription(), o2.getDescription());			
		}		
	}

}
